//
//  SEExamineListViewModel.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/22.
//

import UIKit

class SEExamineListViewModel: SEBaseViewModel {
    var examineList: [SEExamineModel] = []

    var fromType: SEOrderExamineViewControllerFromType = .payexamine

    var cancelHandler: ((SEHttpStatus) -> Void)?

    var aiReporeCreateHandler: ((SEHttpStatus) -> Void)?

    var selectedViewModel = SEExamineDetailViewModel()

    var numberOfSections: Int {
        return examineList.count
    }

    func rowViewModel(indexPath: IndexPath) -> SEExamineDetailViewModel {
        return SEExamineDetailViewModel(model: examineList[indexPath.section])
    }

    /// 检查订单列表
    func requestMyExamineList(selectSource: Int) {
        SEHttpManager.shared.request(SEExamineAPI.examineList(pageNum: page, pageSize: pageSize, selectSource: selectSource), model: SEExamineListModel.self) { (model, status) in
            switch status {
            case .success:
                if self.page == 1 {
                    self.examineList = model?.list ?? []
                } else {
                    self.examineList.append(contentsOf: model?.list ?? [])
                }
                self.hasMore = self.examineList.count < model?.total ?? 0
            default:
                self.page == 1 ? (self.page = 1) : (self.page -= 1)
            }
            self.updateData?(status)
        }
    }

    /// 取消检查预约
    func requestCancelCheck() {
        SEHttpManager.shared.requestForAny(SEExamineAPI.cancelExamine(applyFormId: selectedViewModel.model.id), withLoading: true) { (result, status) in
            self.cancelHandler?(status)
        }
    }

    /// 创建AI报告
    func createExamineAIReport() {
        SEHttpManager.shared.requestForAny(SEExamineAPI.examineAIReportCreate(applyFormId: selectedViewModel.model.id, flag: selectedViewModel.model.provider, templateId: selectedViewModel.model.templateId), withLoading: true) { result, status in
            self.aiReporeCreateHandler?(status)
        }
    }
}
